import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

export const store= new Vuex.Store({
    state: {
        goodsList:[],
        addFoods: false,
        id: 0,   //餐厅id
        shopData: {},   //餐厅全部数据
        order_id: '' //订单ID
    },
    getters: {
        goodsNumber: state => {
            let count = 0
            state.goodsList.forEach( res => {
                count += res.count
            })
            return count
        },
        goodsPrice: state => {
            let price = 0
            state.goodsList.forEach( res => {
                let specPrice = 0
                if(res.spec){
                    res.spec.forEach( val => {
                        specPrice += Number(val.spec_price)
                    })
                    price += (res.price * res.discount + Number(specPrice)) * res.count
                }else{
                    price += res.price * res.discount * res.count
                }
            })
            return price.toFixed(2)
        }
    },
    mutations: {
        addGoods: (state,data)=>{
            state.goodsList.push(data);                      
        },
        joinCart: (state,food) => {
            let noThisFood = true
            let specFlag = true;
            for( let i=0; i<state.goodsList.length;i++){
                if(state.goodsList[i].id == food.id){
                    if(state.goodsList[i].spec){
                        state.goodsList[i].spec.forEach( (val1,h) => {
                            food.spec.forEach( (val2,j) => {
                                if(h==j){
                                    if(val1.spec_name != val2.spec_name){
                                    specFlag = false;
                                    return false;
                                    }
                                }
                            })
                        })
                    }
                }else{
                    specFlag = false
                }
                if(specFlag){
                    state.goodsList[i].count++;
                    console.log('相同:');
                    noThisFood = false
                    break;
                }else{
                    specFlag = true;
                    console.log('不同:')
                }
            }
            if(noThisFood) {
                food.count++;
                state.goodsList.push(food);
            }
        },
        deleteGoods(state,food){
            let specFlag = true;
            for( let i=0; i<state.goodsList.length;i++){
                if(state.goodsList[i].id == food.id){
                    if(state.goodsList[i].spec){
                        state.goodsList[i].spec.forEach( (val1,h) => {
                            food.spec.forEach( (val2,j) => {
                              if(h==j){
                                if(val1.spec_name != val2.spec_name){
                                  specFlag = false;
                                  return false;
                                }
                              }
                            })
                        })
                    }
                }else{
                    specFlag = false
                }
                if(specFlag){
                    state.goodsList.splice(i,1)
                    console.log('相同:')
                    break;
                }else{
                    specFlag = true;
                    console.log('不同:')
                }
            }
        },
        updateGoods(state,data){  
            const {id,key,value,food}=data;
            console.log(food)
            let specFlag = true;
            for( let i=0; i<state.goodsList.length;i++){
                if(state.goodsList[i].id == id){
                    if(state.goodsList[i].spec){
                        state.goodsList[i].spec.forEach( (val1,h) => {
                            food.spec.forEach( (val2,j) => {
                              if(h==j){
                                if(val1.spec_name != val2.spec_name){
                                  specFlag = false;
                                  return false;
                                }
                              }
                            })
                        })
                    }
                }else{
                    specFlag = false
                }
                if(specFlag){
                    state.goodsList[i][key] = value
                    console.log('相同:')
                    break;
                }else{
                    specFlag = true;
                    console.log('不同:')
                }
            }
        },
        clearCart(state) {
            state.goodsList.forEach( (res,i) => {
                res.count = 0
            })
            state.goodsList = []
        },
        isAddFoods: (state) => {
            state.addFoods = true
        },
        getId: (state,id) => {
            state.id = id
        },
        setShopData: (state,data) => {
            state.shopData = data
        },
        setOrderId: (state,id) => {
            state.order_id = id
        }
    }
})